<template>
  <div class="role-modify-wrap">
    <el-dialog :visible.sync="dialogVisible_modify.show" center>
      <div class="show-title">角色ID：{{ role_id }}</div>
      <div class="show-title">角色名称：{{ role_name_check }}</div>
      <div class="center">
        <el-form :inline="true" :model="ruleform" ref="form" :rules="rules">
          <el-form-item label="名称" prop="name">
            <el-input v-model="ruleform.name" placeholder></el-input>
          </el-form-item>
          <br />
          <el-form-item>
            <el-button type="primary" size="small" @click="submitModify('form')"
              >修改</el-button
            >
            <el-button
              type="warning"
              size="small"
              plain
              @click="onReset('form')"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getData, postData } from "@/request/http";

export default {
  name: "audit",
  props: {
    role_id: {
      type: String,
      default: ""
    },
    role_name: {
      type: String,
      default: ""
    },
    dialogVisible_modify: {
      type: Object,
      default() {
        return {
          show: false
        };
      }
    }
  },
  data() {
    return {
      role_id_check: "",
      role_name_check: "",
      ruleform: {
        name: ""
      },
      rules: {
        name: [{ required: true, message: "", trigger: "blur" }]
      }
    };
  },
  watch: {
    role_id: {
      handler(newVal) {
        this.role_id_check = newVal;
      },
      immediate: true
    },
    role_name: {
      handler(newVal) {
        this.role_name_check = newVal;
      },
      immediate: true
    }
  },
  methods: {
    //修改 提交
    submitModify(formName) {
      var that = this;
     // console.log(that.ruleform.name.length);
      if (that.ruleform.name.length == 0) {
        that.$message.error("请输入要修改的名称");
        return;
      }
      that.$refs[formName].validate(valid => {
        if (valid) {
          postData("/adminweb/role/update", {
            role_id: this.role_id_check,
            role_name: that.ruleform.name
          })
            .then(res => {
              if (res.code == 0) {
                that.$message.success("修改成功！");
                this.role_name_check = that.ruleform.name;
                that.$emit("refreshTableList");
              } else {
                that.$message.error(res.code + "，" + res.msg);
              }
            })
            .catch(res => {
              that.$message.error(res.code + "，" + res.msg);
            });
        } else {
          that.$message.error("修改失败");
          return false;
        }
      });
    },
    // 重置
    onReset(val) {
      //this.$emit("reset", true);
      this.$refs[val].resetFields();
    }
  }
};
</script>

<style scoped>
.center {
  margin-top: 30px;
  width: 100%;
  height: 400px;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}
.show-title {
  height: 30px;
  line-height: 30px;
  font-weight: 600;
  color: #676a6d;
}

.role-modify-wrap >>> .el-dialog {
  width: 33%;
}
.role-modify-wrap  >>> .el-dialog--center .el-dialog__body {
  padding: 20px !important;
  height: 300px !important;
}
.role-modify-wrap  >>> .el-dialog{
    margin-top:30vh!important;
}
</style>
